package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.SubjectLessonVideo;
import com.dingreading.cloud.admin.entity.table.SubjectLessonVideoTableDef;
import com.dingreading.cloud.admin.mapper.SubjectLessonVideoMapper;
import com.dingreading.cloud.admin.service.SubjectLessonVideoService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.KitUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2025-10-09
 */
@Service
public class SubjectLessonVideoServiceImpl extends BaseServiceImpl<SubjectLessonVideoMapper, SubjectLessonVideo, SubjectLessonVideoTableDef> implements SubjectLessonVideoService {

    @Override
    protected SubjectLessonVideoTableDef getTable() {
        return SubjectLessonVideoTableDef.subjectLessonVideo;
    }

    @Override
    public R<Object> getList(String lessonUid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.lessonUid.eq(lessonUid))
                .orderBy(table.id.asc());
        List<SubjectLessonVideo> list = getMapper().selectListByQuery(wrapper);
        return R.ok(list);
    }

    @Override
    public R<Object> updateEnabled(String staffUid, String staffName, String uid, Integer enabled) {
        boolean update = UpdateChain.of(SubjectLessonVideo.class)
                .set(table.lastStaffUid, staffUid)
                .set(table.lastStaffName, staffName)
                .set(table.enabled, enabled)
                .where(table.uid.eq(uid))
                .update();
        if (!update)
            return R.fail();
        return R.ok();
    }

    @Override
    public SubjectLessonVideo getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public R<Object> add(String staffUid, String staffName, String lessonUid, String url, String name, Integer videoType) {
        SubjectLessonVideo data = new SubjectLessonVideo();
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        data.setLessonUid(lessonUid);
        data.setUid(KitUtil.getUuid());
        data.setUrl(url);
        data.setName(name);
        data.setEnabled(1);
        data.setVideoType(videoType);
        save(data);
        return R.ok();
    }

    @Override
    public List<SubjectLessonVideo> listByLessonUidIn(List<String> lessonUidList) {
        return getMapper().selectListByCondition(table.lessonUid.in(lessonUidList));
    }

}
